<template>
  <div class='container' style="background-color: #fff">
    <!-- 头部搜索区域 -->
      <el-form :inline="true" :model="page" class="demo-form-inline" style="padding:30px 20px 0 40px"  >
        <el-row type="flex" justify="space-between">
          <el-form-item label="关键字">
             <el-input v-model="page.keyword" placeholder="根据编号进行搜索"></el-input>
          </el-form-item>

          <el-form-item>
           <el-button @click="del">清除</el-button>
           <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <!-- 提示信息区域 -->
     <div class="hint">
      <span class="el-icon-info"></span><p class="el-alert__content">本数据一共有{{page.conts}}条</p>
     </div>
    <!-- 表格区域 -->
   <div>
    <el-table :data="tableData" style="width: 98%" class="table">
      <el-table-column prop="id" label="编号" width="220" class="el-table-column"></el-table-column>
      <el-table-column prop='questionType' label="题型" width="80" style="font-size: 30px !important" :formatter="questionType"></el-table-column>

      <el-table-column  label="题目编号" width="220">
        <template slot-scope="{row}">
          <div v-for="(item, index) in row.questionIDs" :key="index" class="lianjie">{{item.number}}</div>
        </template>
      </el-table-column>

      <el-table-column prop="addTime" label="录入时间" width="180"></el-table-column>
      <el-table-column prop="totalSeconds" label="答题时间(s)" width="165"></el-table-column>
      <el-table-column prop="accuracyRate" label="正确率(%)" width="165"></el-table-column>
      <el-table-column prop="userName" label="录入人" width="165"></el-table-column>
      <el-table-column prop="questionType" label="操作" width="80">
      <template slot-scope="{row}">
         <el-button type="danger" icon="el-icon-delete" circle @click="removeRandoms(row.id)"></el-button>
      </template>
      </el-table-column>
    </el-table>
   </div>
  <!-- 分页系统 -->
    <div class="block" style="margin: 30px 90px 25px 560px;">
    <el-pagination
     background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[20, 30, 50, 100]"
      :page-size="20"
      layout="  prev, pager, next,sizes, jumper"
      :total="page.conts">
    </el-pagination>
  </div>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '../../api/hmmm/questions'
import { questionType } from '../../api/hmmm/constants'
export default {
  data () {
    return {
      questionIDs: '',
      id: '',
      currentPage: 1,
      formInline: {
        user: '',
        region: ''
      },
      tableData: [],
      page: {
        page: 1,
        pagesize: 10,
        keyword: '',
        conts: 0// 总数
      }
    }
  },
  created () {
    this.randoms()
  },
  methods: {

    // 搜索部分
    onSubmit () {
      this.randoms()
    },
    // 页面渲染
    async randoms () {
      // const abc = await randoms(this.page)
      // console.log(abc, 555)
      const { data: res } = await randoms(this.page)
      console.log(res, 999)
      const conts = res.counts
      this.tableData = res.items
      this.page.conts = conts
      this.questionIDs = res.items.questionIDs
      // console.log(this.page.conts, 555)
    },
    // 删除部分
    async removeRandoms (id) {
      // console.log(id)
      try {
        await this.$confirm('确定要删除该数据吗')
        await removeRandoms({ id: id })
        this.randoms()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },
    handleSizeChange (newpagesize) {
      this.page.pagesize = newpagesize
      this.randoms()
    },
    handleCurrentChange (newPage) {
      this.page.page = newPage
      this.randoms()
    },
    // 清除按钮
    del () {
      this.page.keyword = ''
    },
    // 改变题型类型
    questionType (row, column, cellValue, index) {
      const obj = questionType.find(item => item.value === +cellValue)
      return obj ? obj.label : ''
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped >
.container{
  margin-top: 15px;
  margin-left: 15px;
}
.hint{
  font-size: 14px;
  height: 40px;
  background-color: #f4f4f5;
  color: #909399;
  border-radius: 5px;
  display: flex;
  padding-left: 10px;
  margin: 0 25px 0 25px;
}
.el-icon-info{
  display: block;
  margin-top: 15px;
}
.table{
   padding: 25px;
   .el-table-column{
    font-size: 30px !important;
   }
}
.lianjie{
  color: #0099ff;
}

</style>
